package topcoder;

public class UndoHistory {

	int maxprefix(String a, String b) {
		int r = 0;
		for (int i = 0; i < Math.min(a.length(), b.length()); i++) {
			if (a.charAt(i) == b.charAt(i))
				r += 1;
			else
				break;
		}
		return r;
	}
	
	public int minPresses(String[] L) {
		int r = L.length;
		r += L[0].length();
		
		for (int i = 1; i < L.length; i++) {
			int best = L[i].length() + 2;
			
			for (int j = 0; j < i; j++)
				best = Math.min(best, L[i].length() - maxprefix(L[j], L[i]) + 2);
			
			if (maxprefix(L[i-1], L[i]) == L[i-1].length())
				best = Math.min(best, L[i].length() - L[i-1].length());
			
			r += best;	
		}
		
		return r;
	}
	
	public static void main(String[] args) {
		UndoHistory r = new UndoHistory();
		System.out.println(r.minPresses(new String[]{"tomorrow", "topcoder"}));
	}
}
